﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Autodesk.Revit.DB;
using DataBase;

namespace DBService
{
	public class DbEdge : DbObject
	{
		[Category("属性")]
		[Description("起点")]
		[DisplayName("起点")]
		public XYZ StartPt
		{
			get
			{
				return this.mStartPt;
			}
			set
			{
				this.mStartPt = value;
			}
		}

		[Category("属性")]
		[Description("终点")]
		[DisplayName("终点")]
		public XYZ EndPt
		{
			get
			{
				return this.mEndPt;
			}
			set
			{
				this.mEndPt = value;
			}
		}

		[Category("属性")]
		[Description("凸度")]
		[DisplayName("凸度")]
		public double Bulge
		{
			get
			{
				return this.mBulge;
			}
			set
			{
				this.mBulge = value;
			}
		}

		public DbEdge(XYZ startPt, XYZ endPt, double bulge)
		{
			this.mStartPt = startPt;
			this.mEndPt = endPt;
			this.mBulge = bulge;
		}

		public static DbEdge GetFromDB(int id, IDatabase db)
		{
			DbEdge result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT StartPtX,StartPtY,StartPtZ,EndPtX,EndPtY,EndPtZ,Bulge FROM tbedge WHERE Id={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				XYZ startPt = new XYZ(Convert.ToDouble(dataTable.Rows[0]["StartPtX"]), Convert.ToDouble(dataTable.Rows[0]["StartPtY"]), Convert.ToDouble(dataTable.Rows[0]["StartPtZ"]));
				XYZ endPt = new XYZ(Convert.ToDouble(dataTable.Rows[0]["EndPtX"]), Convert.ToDouble(dataTable.Rows[0]["EndPtY"]), Convert.ToDouble(dataTable.Rows[0]["EndPtZ"]));
				double bulge = Convert.ToDouble(dataTable.Rows[0]["Bulge"]);
				result = new DbEdge(startPt, endPt, bulge)
				{
					mId = id,
					mDatabase = db
				};
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbedge (StartPtX, StartPtY,StartPtZ,EndPtX,EndPtY,EndPtZ,Bulge) VALUES ({0},{1},{2},{3},{4},{5},{6})", new object[]
				{
					this.mStartPt.X,
					this.mStartPt.Y,
					this.mStartPt.Z,
					this.mEndPt.X,
					this.mEndPt.Y,
					this.mEndPt.Z,
					this.mBulge
				});
				db.ExecuteCommand(sql);
				this.mDatabase = db;
				string sql2 = string.Format("select max(Id) from tbedge", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbedge SET StartPtX = {0},StartPtY={1},StartPtZ={2},EndPtX={3},EndPtY={4},EndPtZ={5},Bulge={6} WHERE Id ={7}", new object[]
				{
					this.mStartPt.X,
					this.mStartPt.Y,
					this.mStartPt.Z,
					this.mEndPt.X,
					this.mEndPt.Y,
					this.mEndPt.Z,
					this.mBulge,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbedge WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public void Remove(DbTransaction tr)
		{
			try
			{
				string sql = string.Format("DELETE FROM tbedge WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql, tr);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private XYZ mStartPt;

		private XYZ mEndPt;

		private double mBulge;
	}
}
